package cn.leonis.leetcode;

import java.util.Arrays;

/**
 * @Author Tobi
 * @Date 2022-07-13 下午2:24
 * leetcode 1480 一维数组的动态和
 */
public class RunningSumArraySolution {

    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 1, 1};
        RunningSumArraySolution solution = new RunningSumArraySolution();
        int[] ints = solution.runningSum1(nums);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }

    /**
     * 暴力循环，空间耗费比较多
     * @param nums
     * @return
     */
    public int[] runningSum(int[] nums) {
        int[] result = new int[nums.length];
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            result[i] = sum;
        }
        return result;
    }

    /**
     * 原地修改
     * @param nums
     * @return
     */
    public int[] runningSum1(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            nums[i] += nums[i - 1];
        }
        return nums;
    }
}
